package com.example.yyy


import android.graphics.Bitmap
import android.graphics.BitmapFactory
import java.io.File
import java.io.FileInputStream
import java.io.FileNotFoundException
import java.io.FileOutputStream

class FileUtil {

    //保存文本文件
    @Throws(Exception::class)
    fun saveText(path: String, txt: String){
        val fos = FileOutputStream(path)
        fos.write(txt.toByteArray())
        fos.close()
    }

    //读取文本文件
    @Throws(Exception::class,FileNotFoundException::class)
    fun openText(path: String): String? {
        var readStr: String? = ""
        val fis = FileInputStream(path)
        val b = ByteArray(fis.available())
        fis.read(b)
        readStr = String(b)
        fis.close()
        return readStr
    }
    //保存图片文件
    @Throws(Exception::class)
     fun saveImage(path: String, bitmap: Bitmap){
        val fos = File(path).outputStream()
        //压缩格式为JPEG头像，压缩质量为80%
        bitmap.compress(Bitmap.CompressFormat.JPEG,80,fos)
        fos.flush()
        fos.close()
    }

    //读取图片文件
    fun openImage(path: String):Bitmap{
        var bitmap:Bitmap? = null
        val fis = FileInputStream(path)
        //从输入流解析图片
        bitmap = BitmapFactory.decodeStream(fis)
        fis.close()
        return bitmap
    }
}